﻿using System;
using System.Web.Security;
using System.Web.UI;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.FrontEnd.inside.Editor.Video
{
    public partial class VideoUpdate : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Request.QueryString["ID"] == null) return;
            if (!Common.Instance.IsNumeric(Request.QueryString["ID"])) return;

            var videoItem = Core.BLL.Videos.Instance.GetById(Convert.ToInt32(Request.QueryString["ID"]));
            txtBody.Value = Server.HtmlDecode(videoItem.VideoDesc);
            txtTitle.Text = videoItem.VideoTitle;
            drpStatus.SelectedValue = videoItem.VideoStatus ? "1" : "0";

            var createDate = Convert.ToDateTime(videoItem.CreateDate);
            var membershipUser = Membership.GetUser(videoItem.UserId);
            if (membershipUser != null)
            {
                imgVideo.ImageUrl = string.Format("/Uploads/{0}/{1}/{2}/{3}/{4}{5}", membershipUser.UserName,
                                                  createDate.Year,
                                                  Common.Instance.Right("0" + createDate.Month, 2),
                                                  Common.Instance.Right("0" + createDate.Day, 2), "100x0__",
                                                  videoItem.VideoImage);
            }
            divVideo.InnerHtml =
                "<iframe width=\"459\" height=\"344\" src=\"http://www.youtube.com/embed/" + videoItem.VideoLink +
                "?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>";
        }

        protected void BtnUpdate_OnClick(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] == null) return;
            if (!Common.Instance.IsNumeric(Request.QueryString["ID"])) return;
            Core.BLL.Videos.Instance.Update(Convert.ToInt32(Request.QueryString["ID"]), txtTitle.Text, Server.HtmlEncode(txtBody.Value),
                                            drpStatus.SelectedValue == "1");
            Response.Redirect("Default.aspx");
        }
    }
}